/*
 * Copyright 2007 Ulf Ochsenfahrt <ulf@ofahrt.de>
 *
 * This file is part of mnemoj, a software transactional memory implementation
 * that is based on Project Darkstar.
 *
 * mnemoj is free software: you can redistribute it
 * and/or modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation and
 * distributed hereunder to you.
 *
 * Project Darkstar Server is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package de.ofahrt.mnemoj.impl;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import com.sun.sgs.app.ManagedObject;
import com.sun.sgs.app.ObjectNotFoundException;

final class ManagedEntry
{

private final Long id;
private final Lock lock = new ReentrantLock();
private byte[] serializedForm;

ManagedEntry(Long id)
{
	this.id = id;
	this.serializedForm = null;
}

public Long getId()
{ return id; }

public Lock getLock()
{ return lock; }

public ManagedObject getCopy()
{
	if (serializedForm == null)
		throw new ObjectNotFoundException();
	return (ManagedObject) SerializationHelper.deserialize(serializedForm);
}

public void set(byte[] newdata)
{
	if (newdata == null) throw new NullPointerException();
	serializedForm = newdata;
}

public boolean isNew()
{ return serializedForm == null; }

}
